﻿using Abp.Linq.Expressions;
using Common.Enums;
using Entity.Models.dto;
using Entity.Models.Entities;
using Entity.Models.vo;
using IBLL;
using IDAL;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using utils.enums;
using utils.Enums;
using utils.EX;
using Utils;

namespace BLL
{
    /// <summary>
    /// 工作流模板BLL层
    /// </summary>
    public class WorkFlow_ModelBLL : IWorkFlow_ModelBLL
    {
        IWorkFlow_ModelDAL _workFlow_ModelDal;

        /// <summary>
        /// 数据库上下文
        /// </summary>
        RepositorySysContext _dbsysContext;

        /// <summary>
        /// 用户接口
        /// </summary>
        IUserDAL _userDAL;



        /// <summary>
        /// 工作流模板记录DAL接口
        /// </summary>
        IConsumableRecordDAL _consumableRecordDAL;

        public WorkFlow_ModelBLL(IWorkFlow_ModelDAL workFlow_Model, IUserDAL userDAL, IConsumableRecordDAL consumableRecordDAL, RepositorySysContext dbsysContext)
        {
            _workFlow_ModelDal = workFlow_Model;
            _userDAL = userDAL;
            _consumableRecordDAL = consumableRecordDAL;
            _dbsysContext = dbsysContext;
        }

        /// <summary>
        /// 工作流模板数据列表、搜索
        /// </summary>
        /// <param name="title">工作流模板名称</param>
        /// <param name="page">页数</param>
        /// <param name="limit">数据条数</param>
        /// <returns></returns>
        public List<WorkFlow_ModelDTO> getworkFlow_ModelList(string title, string page, string limit, out int cont)
        {
            //连表
             IQueryable<WorkFlow_Model> datas = _workFlow_ModelDal.getEntitys().Where(d => d.IsDelete != true);

            //搜索
            if (!string.IsNullOrEmpty(title))
            {
                datas = datas.Where(i => i.Title.Contains(title));
            }
            //分页
            var pageworkFlow_Model = datas.OrderByDescending(d => d.CreateTime).Skip((int.Parse(page) - 1) * int.Parse(limit)).Take(int.Parse(limit)).ToList();
            cont = datas.Count();

            List<WorkFlow_ModelDTO> workFlow_ModelDTOs = new List<WorkFlow_ModelDTO>();
            foreach (var item in pageworkFlow_Model)
            {
                workFlow_ModelDTOs.Add(new WorkFlow_ModelDTO(item));
            }
            return workFlow_ModelDTOs;
        }

        /// <summary>
        /// 添加工作流模板
        /// </summary>
        /// <param name="workFlow_Model"></param>
        /// <returns></returns>。
        public bool AddWorkFlow_Model(WorkFlow_Model workFlow_Model)
        {
            workFlow_Model.Id = Guid.NewGuid().ToString();
            workFlow_Model.CreateTime = DateTime.Now;
            //根据信息
            WorkFlow_Model byworkFlow_ModelInfo = _workFlow_ModelDal.getEntitys().FirstOrDefault(d => d.IsDelete != true && d.Title == workFlow_Model.Title);
            if (byworkFlow_ModelInfo == null)
            {
                //保存
                if (_workFlow_ModelDal.AddEntity(workFlow_Model) < 0)
                {
                    throw new MyException(((int)UserEnums.USER_STATE.LOSE), Enums.getDescribe(UserEnums.USER_STATE.LOSE));
                }
            }
            else
            {
                throw new MyException(800, "模板已存在");
            }
            return true;
        }

        /// <summary>
        /// 删除工作流模板
        /// </summary>
        /// <param name="workFlow_ModelId">工作流模板id</param>
        /// <returns></returns>
        public bool DalateWorkFlow_Model(string workFlow_ModelId)
        {
            //删除
            if (_workFlow_ModelDal.SoftDalateEntity(workFlow_ModelId) < 0)
            {
                throw new MyException(((int)UserEnums.USER_STATE.DATATE_LOSE), Enums.getDescribe(UserEnums.USER_STATE.DATATE_LOSE));
            }
            return true;
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="workFlow_ModelIds"></param>
        /// <returns></returns>
        public bool DalateWorkFlow_ModelList(List<String> workFlow_ModelIds)
        {
            if (workFlow_ModelIds.Count < 1)
            {
                throw new MyException(((int)UserEnums.USER_STATE.UPDATE_LIST), Enums.getDescribe(UserEnums.USER_STATE.UPDATE_LIST));
            }
            foreach (var item in workFlow_ModelIds)
            {
                DalateWorkFlow_Model(item);
            }
            return true;
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="workFlow_Model"></param>
        /// <returns></returns>
        public bool UpdataWorkFlow_Model(WorkFlow_Model workFlow_Model)
        {
            var workFlow_Models = _workFlow_ModelDal.getByEntityId(workFlow_Model.Id);
            workFlow_Models.Title = workFlow_Model.Title;
            workFlow_Models.Description= workFlow_Model.Description;
            if (_workFlow_ModelDal.UpdataEntity(workFlow_Models) < 0)
            {
                throw new MyException(((int)UserEnums.USER_STATE.UPDATE_LOSE), Enums.getDescribe(UserEnums.USER_STATE.UPDATE_LOSE));
            }
            return true;
        }


        /// <summary>
        /// 根据工作流模板id搜索
        /// </summary>
        /// <param name="workFlow_Modelid"></param>
        /// <returns></returns>
        public WorkFlow_Model getByWorkFlow_Model(string workFlow_Modelid)
        {
            return _workFlow_ModelDal.getByEntityId(workFlow_Modelid);
        }

        /// <summary>
        /// 工作流模板下拉框
        /// </summary>
        /// <returns></returns>
        public List<TransferVo> getWorkFlow_ModelSelect()
        {
            return _workFlow_ModelDal.getEntitys().Where(w => w.IsDelete != true).Select(w => new TransferVo(w)).ToList();
        }
    }
}
